package com.pig4cloud.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.mall.product.dto.QueryReq;
import com.pig4cloud.mall.product.entity.AttrGroup;
import com.pig4cloud.mall.product.mapper.AttrGroupMapper;
import com.pig4cloud.mall.product.service.AttrGroupService;
import com.pig4cloud.pig.common.core.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 属性分组
 *
 * @author fangyu
 * @date 2020-11-25 15:05:25
 */
@Service
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroup> implements AttrGroupService {

	@Override
	public PageResult queryPage(Map<String, Object> params, Long catId) {
		IPage<AttrGroup> page;
		if (catId == 0) {
			page = this.page(new QueryReq<AttrGroup>().getPage(params), new QueryWrapper<AttrGroup>());
		} else {
			// select * from pms_attr_group where
			QueryWrapper<AttrGroup> wrapper = new QueryWrapper<AttrGroup>().eq("catelog_id", catId);
			String key = (String) params.get("key");
			if (!StringUtils.isEmpty(key)) {
				wrapper.and((obj)->{
					obj.eq("attr_group_id", key).or().like("attr_group_name", key);
				});
			}
			 page = this.page(new QueryReq<AttrGroup>().getPage(params), wrapper);
		}
		return PageResult.buildPage(page);
	}
}
